import { useStore } from 'vuex'
import { reactive, toRefs, getCurrentInstance } from 'vue'
// import app from '@/plugins/index'
export const useLanguageEffect = () => {
  const store = useStore()
  const state = reactive({
    value: localStorage.getItem('locale') || 'zh'
  })

  console.log('getCurrentInstance:', getCurrentInstance())
  const app = getCurrentInstance()

  const option = [
    { text: '中文', value: 'zh' },
    { text: '英文', value: 'en' }
  ]
  // 监听下拉框变化
  const changeLanguage = (value: string) => {
    ;(app as any).ctx.value = value
    // ;(app as any).ctx.$i18n.locale = value
    store.commit('setLanguage', (app as any).ctx.$i18n)
    // 强制刷新页面
    // window.location.reload()
  }
  const { value } = toRefs(state)
  return {
    changeLanguage,
    option,
    value
  }
}
